﻿// This code is released under the BSD license.
namespace PugLib
{
    using System;
    using System.Globalization;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading;

    /// <summary>
    /// A collection of common string utility methods
    /// </summary>
    public static class StringUtilities
    {
        public static bool IsEmpty(object input)
        {
            return input == null || Convert.ToString(input, CultureInfo.CurrentCulture).Length == 0;
        }

        public static bool IsNumeric(object input)
        {
            return RegexUtils.IsExactMatch(CutWhiteSpace(Convert.ToString(input, CultureInfo.CurrentCulture)), RegexUtils.RegexNumeric);
        }

        public static bool IsEmail(string input)
        {
            return RegexUtils.IsExactMatch(input, RegexUtils.RegexEmail);
        }

        //just to take nulls into consideration
        public static string Trim(string input)
        {
            if (IsEmpty(input))
            {
                return input;
            }
            return input.Trim();
        }

        public static string CutWhiteSpace(string input)
        {
            if (IsEmpty(input))
            {
                return input;
            }
            return Trim(Regex.Replace(input, @"\s+", " "));
        }

        public static string CutEnd(string input, int length)
        {
            if (IsEmpty(input))
            {
                return input;
            }
            if (input.Length <= length)
            {
                return string.Empty;
            }
            return input.Substring(0, input.Length - length);
        }

        public static string CutStart(string input, int length)
        {
            if (IsEmpty(input))
            {
                return input;
            }
            if (input.Length <= length)
            {
                return string.Empty;
            }
            return input.Substring(length);
        }

        public static string Start(string input, int length)
        {
            if (IsEmpty(input))
            {
                return input;
            }
            if (input.Length <= length)
            {
                return input;
            }
            return input.Substring(0, length);
        }

        public static string End(string input, int length)
        {
            if (IsEmpty(input))
            {
                return input;
            }
            if (input.Length <= length)
            {
                return input;
            }
            return input.Substring(input.Length - length);
        }

        public static string ToTitleCase(string input)
        {
            return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(input);
        }

        /// <summary>
        /// returns a friendly name of a string
        /// eg. "SomeUtilsText" becomes "Some Utils Text"        
        /// </summary>
        /// <param name="input">the input string</param>        
        /// <returns>a friendly name string</returns>
        public static string ToFriendlyName(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return input;
            }
            input = input.Trim(); //trim it
            if (input.ToUpper(CultureInfo.CurrentCulture) == input)
            {
                return input; //if its all capitals we cant do anything with it
            }
            StringBuilder sb = new StringBuilder(input.Length);
            char? last = null;
            foreach (char c in input)
            {
                if (last != null && Char.IsUpper(c)) // && Char.IsLower(last ?? char.MinValue))
                {
                    sb.Append(" ").Append(c);
                }
                else
                {
                    sb.Append(c);
                }
                last = c;
            }
            return sb.ToString();
        }
    }
}